﻿
Public Class PanelFilters

    'Load :
    Private Sub PanelFilters_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        _Filters = New List(Of Filter)
        _Filters.Add(Filter.DefaultFilter)
        ShowFilters(0)
    End Sub

#Region "Filters List"
    'Fields :
    Private _Filters As List(Of Filter)
    Public ReadOnly Property Filters As List(Of Filter)
        Get
            Return _Filters
        End Get
    End Property
    'Routines:
    Private Sub ShowFilters(ByVal selctindex As Integer)
        Filters_KLB.Items.Clear()
        For Each fil As Filter In _Filters
            Filters_KLB.Items.Add(fil.Name)
        Next
        Filters_KLB.SelectedIndex = selctindex
    End Sub
    'Selected Filter Changed :
    Private Sub Filters_KLB_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles Filters_KLB.SelectedIndexChanged
        If Filters_KLB.SelectedIndex = 0 Then
            Filter_KH.Enabled = False
            Filter_KTB.Enabled = False
            Filter_KG.Enabled = False
        Else
            Filter_KH.Enabled = True
            Filter_KTB.Enabled = True
            Filter_KG.Enabled = True
        End If
        If Filters_KLB.SelectedIndex = 0 Then
            Filters_B_Delete.Enabled = ButtonEnabled.False
        Else
            Filters_B_Delete.Enabled = ButtonEnabled.True
        End If
        _CurrentFilter = _Filters.Item(Filters_KLB.SelectedIndex)
        ShowFilter()
    End Sub
    'Actions :
    Private Sub Filters_B_Delete_Click(sender As System.Object, e As System.EventArgs) Handles Filters_B_Delete.Click
        _Filters.RemoveAt(Filters_KLB.SelectedIndex)
        ShowFilters(0)
    End Sub
    Private Sub Filters_B_Add_Click(sender As System.Object, e As System.EventArgs) Handles Filters_B_Add.Click
        Filters_KH.Enabled = False
        Filters_KLB.Enabled = False
        Filters_KTB.Clear()
        Filters_KTB.Visible = True
        Filters_KTB.Select()
    End Sub
    Private Sub Filters_B_Accept_Click(sender As System.Object, e As System.EventArgs) Handles Filters_B_Accept.Click
        If String.IsNullOrWhiteSpace(Filters_KTB.Text) = False Then
            Dim fil As New Filter With {.Name = Filters_KTB.Text}
            _Filters.Add(fil)
            Dim index As Integer = _Filters.Count - 1
            Filters_KH.Enabled = True
            Filters_KLB.Enabled = True
            Filters_KTB.Clear()
            Filters_KTB.Visible = False
            ShowFilters(index)
        End If
    End Sub
    Private Sub Filters_B_Cancel_Click(sender As System.Object, e As System.EventArgs) Handles Filters_B_Cancel.Click
        Filters_KH.Enabled = True
        Filters_KLB.Enabled = True
        Filters_KTB.Clear()
        Filters_KTB.Visible = False
    End Sub
#End Region
#Region "Current Filter"
    'Current Filter :
    Private _CurrentFilter As New Filter
    'Show Filter :
    Private Sub ShowFilter()
        Me.SuspendLayout()
        Filter_KH.Values.Heading = _CurrentFilter.Name
        Filter_KLB.Items.Clear()
        For Each ext As String In _CurrentFilter.FilteredExtensions
            Filter_KLB.Items.Add(ext)
        Next
        If Filter_KLB.Items.Count > 0 Then
            Filter_KLB.SelectedIndex = 0
            Filter_B_Delete.Enabled = ButtonEnabled.True
        Else
            Filter_B_Delete.Enabled = ButtonEnabled.False
        End If
        Filter_KCB_Method.SelectedIndex = _CurrentFilter.Method.ToIndex
        Filter_KCB_Level.SelectedIndex = CInt(_CurrentFilter.Level)
        Me.ResumeLayout()
    End Sub
    'Selection :
    Private Sub Filter_KCB_Method_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles Filter_KCB_Method.SelectedIndexChanged
        Select Case Filter_KCB_Method.SelectedIndex
            Case 0
                _CurrentFilter.Method = Zip.CompressionMethod.None
            Case 1
                _CurrentFilter.Method = Zip.CompressionMethod.Deflate
            Case 2
                _CurrentFilter.Method = Zip.CompressionMethod.BZip2
        End Select
        ShowFilter()
    End Sub
    Private Sub Filter_KCB_Level_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles Filter_KCB_Level.SelectedIndexChanged
        _CurrentFilter.Level = CType(Filter_KCB_Level.SelectedIndex, Zlib.CompressionLevel)
        ShowFilter()
    End Sub
    'Actions :
    Private Sub Filter_B_Delete_Click(sender As System.Object, e As System.EventArgs) Handles Filter_B_Delete.Click
        _CurrentFilter.FilteredExtensions.RemoveAt(Filter_KLB.SelectedIndex)
        ShowFilter()
    End Sub
    Private Sub Filter_B_Add_Click(sender As System.Object, e As System.EventArgs) Handles Filter_B_Add.Click
        Me.SuspendLayout()
        Filter_KH.Enabled = False
        Filter_KCB_Method.Enabled = False
        Filter_KCB_Level.Enabled = False
        Filter_KLB.Enabled = False
        Filter_KTB.Clear()
        Filter_KTB.Visible = True
        Filter_KTB.Select()
        Me.ResumeLayout()
    End Sub
    Private Sub Filter_B_Ok_Click(sender As System.Object, e As System.EventArgs) Handles Filter_B_Ok.Click
        Me.SuspendLayout()
        If String.IsNullOrWhiteSpace(Filter_KTB.Text) = False Then
            If Filter_KTB.Text.StartsWith(".") = False Then
                Filter_KTB.Text = "." & Filter_KTB.Text
            End If
        End If
        If _CurrentFilter.FilteredExtensions.Contains(Filter_KTB.Text) = False Then
            _CurrentFilter.FilteredExtensions.Add(Filter_KTB.Text)
        End If
        Filter_KH.Enabled = True
        Filter_KCB_Method.Enabled = True
        Filter_KCB_Level.Enabled = True
        Filter_KLB.Enabled = True
        Filter_KTB.Clear()
        Filter_KTB.Visible = False
        ShowFilter()
        Me.ResumeLayout()
    End Sub
    Private Sub Filter_B_Cancel_Click(sender As System.Object, e As System.EventArgs) Handles Filter_B_Cancel.Click
        Me.SuspendLayout()
        Filter_KH.Enabled = True
        Filter_KCB_Method.Enabled = True
        Filter_KCB_Level.Enabled = True
        Filter_KLB.Enabled = True
        Filter_KTB.Clear()
        Filter_KTB.Visible = False
        Me.ResumeLayout()
    End Sub
#End Region

   
End Class
